<?php
/**
 * MYSQL数据库分页
 *
 * $Id: Pager.class.php 89 2009-08-25 07:40:55Z sunxy $
 */

class Pager {
    var $PageSize;              // 每页的数量
    var $CurrentPageID;         // 当前的页数
    var $NextPageID;            // 下一页
    var $PreviousPageID;        // 上一页
    var $numPages;              // 总页数即末一页
    var $numItems;              // 总记录数
    var $isFirstPage;           // 是否第一页
    var $isLastPage;            // 是否未一页
    var $db;                    // mysql数据库操作
    var $sql;                   // sql查询语句
    var $from;                  // 记录起始值
    var $number;                // 记录数

    /**
     * 构造函数
     * @param array 关联数组
     */
    function Pager($option)
    {
        $this->_setOptions($option);

        if (!isset($this->numItems)) {
            $res = $this->db->query($this->sql);
            $this->numItems = $this->db->num_rows($res);
        }

        // 总页数
        if ($this->numItems > 0) {
            if ($this->numItems < $this->PageSize) {
                $this->numPages = 1;
            }

            if ($this->numItems % $this->PageSize) {
                $this->numPages = (int)($this->numItems / $this->PageSize) + 1;
            } else {
                $this->numPages = $this->numItems / $this->PageSize;
            }
        } else {
            $this->numPages = 0;
        }

        if ($this->CurrentPageID == -1) {
            $this->CurrentPageID = $this->numPages;
        }

        switch ($this->CurrentPageID) {
            case $this->numPages == 1:
                $this->isFirstPage = true;
                $this->isLastPage = true;
                break;
            case 1:
                $this->isFirstPage = true;
                $this->isLastPage = false;
                break;
            case $this->numPages:
                $this->isFirstPage = false;
                $this->isLastPage = true;
                break;
            default:
                $this->isFirstPage = false;
                $this->isLastPage = false;
        }

        if ($this->numPages > 1) {
            if (!$this->isLastPage) {
                $this->NextPageID = $this->CurrentPageID + 1;
            }
            if (!$this->isFirstPage) {
                $this->PreviousPageID = $this->CurrentPageID - 1;
            }
        }

        return true;
    }

    /**
     * 设置参数
     * @param array 关联数组
     */
    function _setOptions($option) {
        $allow_options = array(
        	'db',
        	'sql',
            'PageSize',
            'CurrentPageID',            
            'numItems'
        );

        foreach ($option as $key => $value) {
            if (in_array($key, $allow_options) && ($value != null)) {
                $this->$key = $value;
            }
        }
        if (!isset($this->CurrentPageID)) {
            $this->CurrentPageID = 1;
        }
        //$this->sql = urldecode($this->sql);
        return true;
    }

    /**
     * 获得一个数据库资源标识符
     * @return boolean 数据库资源标识符
     */
    function getDataLink()
    {
        if ($this->numItems) {        	
            $PageID = $this->CurrentPageID;

            $this->from = ($PageID - 1) * $this->PageSize;            
            $link = $this->db->limitQuery($this->sql, $this->from, $this->PageSize);
            return $link;
        } else {
            return false;
        }
    }

    /**
     * 获得一个数组
     */
    function getPageData() {
    	$this->number = 0;
        if ($this->numItems) {
            if ($res = $this->getDataLink()) {
                if ($this->db->num_rows($res)) {
                    while ($row = $this->db->fetch_array($res)) {
                        $result[] = $row;
                    }
                    $this->number = count($result);
                } else {
                    $result = array();
                }
                return $result;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    /**
     * 获得分页链接(wap格式)
     * @param sting 链接基准
     * @return sting 分页链接
     */
    function getPagination($base, $basepg, $pagename) {
    	$ret = "<br/>第".$this->CurrentPageID."页/共".$this->numPages."页 共".$this->numItems."条<br/>";
		if (!$this->isLastPage) {
			$ret .= "<a href=\"".$base."&amp;".$pagename."=".$this->NextPageID."&amp;numItems=".$this->numItems."\">下一页</a> ";
			$ret .= "<a href=\"".$base."&amp;".$pagename."=".$this->numPages."&amp;numItems=".$this->numItems."\">末一页</a><br/>";
		}

		if (!$this->isFirstPage) {
			$ret .= "<a href=\"".$base."&amp;".$pagename."=".$this->PreviousPageID."&amp;numItems=".$this->numItems."\">上一页</a> ";
			$ret .= "<a href=\"".$base."&amp;".$pagename."=1&amp;numItems=".$this->numItems."\">第一页</a><br/>";
		}
		
		if ($this->numPages > 5) {
			$ret .= "<a href=\"".$basepg."&amp;".$pagename."=".$this->CurrentPageID."\">按页搜</a><br/><br/>";
		} else {
			$ret .= "<br/>";
		}		
		return $ret;
    }	
}
?>